﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace Zeny.Common.Utils
{
    public class XmlUtils
    {
        public static void createNode(XmlDocument xmlDoc, XmlElement parentNode, String name, String value)
        {
            XmlElement node = xmlDoc.CreateElement(name);
            parentNode.AppendChild(node);
            node.InnerText = value;
        }

        public static string getChildNodeInnerText(XmlNode node, String childNodeName)
        {
            XmlNode childNode = node.SelectSingleNode(childNodeName);

            if (childNode == null)
            {
                return null;
            }
            else
            {
                return childNode.InnerText;
            }
        }

        public static void CreateNode(string xmlpath, string node, string value)
        {
            if (!File.Exists(xmlpath))
            {
                XmlDocument xmlDoc = new XmlDocument();
                XmlElement root = xmlDoc.CreateElement("productManage");
                XmlElement child = xmlDoc.CreateElement(node);
                child.InnerText = value;
                root.AppendChild(child);
                xmlDoc.AppendChild(root);
                xmlDoc.Save(xmlpath);
            }
            else
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(xmlpath);
                XmlNode root = xmlDoc.DocumentElement;
                XmlNode child = root.SelectSingleNode(node);
                if (child == null)
                {
                    child = xmlDoc.CreateElement(node);
                }
                child.InnerText = value;
                root.AppendChild(child);
                xmlDoc.AppendChild(root);
                xmlDoc.Save(xmlpath);
            }
        }

        public static XmlNode ReadSingleNode(StreamReader inputStream, string xpath)
        {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(inputStream);
                XmlElement root = xmlDoc.DocumentElement;
                XmlNode child = root.SelectSingleNode(xpath);
                return child;
        }

        public static XmlNodeList ReadMultiNode(StreamReader inputStream, String xpath)
        {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(inputStream);
                XmlElement root = xmlDoc.DocumentElement;
                XmlNodeList childList = root.SelectNodes(xpath);
                return childList;
        }

        public static String getSubNodeInnerText(XmlNode xmlNode, String subNodeName)
        {
            if(xmlNode == null)
            {
                return null;
            }

            XmlNode subXmlNode = xmlNode.SelectSingleNode(subNodeName);
            if (subXmlNode == null)
            {
                return null;
            }

            return subXmlNode.InnerText;
        }
    }
}
